package com.onesports.intelligent.k12.polarlight.service.sys;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.framework.kit.common.util.ParamUtils;
import com.onesports.intelligent.k12.polarlight.constant.BizConstants;
import com.onesports.intelligent.k12.polarlight.domain.entity.sys.Menu;
import com.onesports.intelligent.k12.polarlight.domain.entity.sys.RoleMenu;
import com.onesports.intelligent.k12.polarlight.mapper.sys.MenuMapper;
import com.onesports.intelligent.k12.polarlight.mapper.sys.RoleMenuMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 角色菜单表
 *
 * @author
 * @since 1.0.0 2022-02-11
 */
@Service
@RequiredArgsConstructor
public class RoleMenuService extends ServiceImpl<RoleMenuMapper, RoleMenu> {

    private final  RoleMenuMapper mapper;

    private final MenuMapper menuMapper;


    /**
     * 根据角色id获取角色菜单权限
     * @param roleId 角色id
     * @return List<RoleMenu>
     */
    public List<RoleMenu> getRoleMenuByRoleId(String roleId){
        LambdaQueryWrapper<RoleMenu> roleMenuQuery = new QueryWrapper<RoleMenu>().lambda()
                .eq(RoleMenu::getRoleId,roleId);
        return this.mapper.selectList(roleMenuQuery);
    }



    /**
     * 根据角色id获取角色菜单权限
     * @param roleIds 角色id
     * @return List<RoleMenu>
     */
    public List<Menu> getRoleMenuByRoleId(List<String> roleIds){
        List<Menu> menus = this.mapper.selectRoleMenu(roleIds);

        return menus;
//        Map<String, List<Menu>> menuMap = menus.stream()
//                .filter(f -> ParamUtils.isNotEmpty(f.getSystemCode()))
//                .collect(Collectors.groupingBy(Menu::getRoleId));
//        List<Menu> rmenus = Lists.newArrayList();
//        menuMap.forEach((key,value)->{
//            rmenus.addAll(getParentMenuList(value,key));
//        });
//        Set<Menu> menuSet = new TreeSet<>(Comparator.comparing(Menu::getId));
//        menuSet.addAll(rmenus);
//        return new ArrayList<>(menuSet);
    }

//    public List<Menu> getParentMenuList(List<Menu> menus,String roleId){
//        if(CollectionUtils.isEmpty(menus)){
//            return menus;
//        }
//        List<Menu> haveParentMenuList = menus.stream().filter(menu -> !BizConstants.MENU_ROOT_ID.equals(menu.getParentId())).collect(Collectors.toList());
//        if(CollectionUtils.isEmpty(haveParentMenuList)){
//            return menus;
//        }
//        List<String> parentIds = menus.stream().map(Menu::getParentId).collect(Collectors.toList());
//
//        List<Menu> parentMenus = menuMapper.selectBatchIds(parentIds);
//        parentMenus.forEach(menu -> menu.setRoleId(roleId));
//        menus.addAll(getParentMenuList(parentMenus,roleId));
//        return menus;
//    }





}
